var JWB = {
    utils: {
        fromSerialize: function (form) {
            var o = {};
            $.each(form.serializeArray(), function (index) {
                if (this['value'] != null && this['value'] != "") {
                    if (o[this['name']]) {
                        o[this['name']] = o[this['name']] + "," + this['value'];
                    } else {
                        o[this['name']] = this['value'];
                    }
                }
            });
            return o;
        }
    },
    http: {
        uploadImgPage: function (relationId, moduleCode, callBack, doc) {
            // alert("图片上传的操作比较耗时，请不要中途刷新或者关闭浏览器！");
            var html = "";
            if (doc) {
                html = "        <div class=\"modal-content\">\n" +
                    "            <div class=\"text-center\">\n" +
                    "                <div class=\"modal-header\">\n" +
                    "                 </div>\n" +
                    "                <br>\n" +
                    "                <form enctype=\"multipart/form-data\">\n" +
                    "                   <div class=\"file-loading\">\n" +
                    "                       <input name='file' id=\"file-0a\" class=\"file\" type=\"file\" multiple data-min-file-count=\"1\">\n" +
                    "                  </div>\n" +
                    "                </form>\n" +
                    "            </div>\n" +
                    "        </div>\n";
                $(doc).append(html);
            } else {

                html = "<div class=\"cover\" id=\"uploadFloatageWindow\">\n" +
                    "    <div class=\"modal-dialog floatage\">\n" +
                    "        <div class=\"modal-content\">\n" +
                    "            <div class=\"text-center\">\n" +
                    "                <div class=\"modal-header\">\n" +
                    "                    <button type=\"button\" class=\"close\" onclick=\"$('#uploadFloatageWindow').remove();\">x</button>\n" +
                    "                 </div>\n" +
                    "                <br>\n" +
                    "                <form enctype=\"multipart/form-data\">\n" +
                    "                   <div class=\"file-loading\">\n" +
                    "                       <input name='file' id=\"file-0a\" class=\"file\" type=\"file\" multiple data-min-file-count=\"1\">\n" +
                    "                  </div>\n" +
                    "                </form>\n" +
                    "            </div>\n" +
                    "        </div>\n" +
                    "    </div>\n" +
                    "</div>";
                $("body").append(html);
            }


            var url = base.fileUrl + "sysfile/upload";
            if (relationId != null) {
                if (url.indexOf("?") == -1) {
                    url += "?sysFile.relationId=" + relationId;
                } else {
                    url += "&sysFile.relationId=" + relationId;
                }
            }
            if (moduleCode != null) {
                if (url.indexOf("?") == -1) {
                    url += "?sysFile.moduleCode=" + moduleCode;
                } else {
                    url += "&sysFile.moduleCode=" + moduleCode;
                }
            }
            var fileInput = $("#file-0a");
            fileInput.fileinput({

                language: 'zh', //设置语言
                uploadUrl: url, //上传的地址

                allowedFileExtensions: ['jpg', 'gif', 'png'],//接收的文件后缀

                //uploadExtraData:{"id": 1, "fileName":'123.mp3'},

                uploadAsync: true, //默认异步上传

                showUpload: true, //是否显示上传按钮

                showRemove: true, //显示移除按钮

                showPreview: true, //是否显示预览

                // showCaption: false,//是否显示标题

                browseClass: "btn btn-primary", //按钮样式

                dropZoneEnabled: false,//是否显示拖拽区域

                maxFileSize: 1024,//单位为kb，如果为0表示不限制文件大小

                //minFileCount: 0,

                maxFileCount: 1, //表示允许同时上传的最大文件个数
                autoReplace: true,// 是否自动替换当前图片，设置为true时，再次选择文件， 会将当前的文件替换掉

                enctype: 'multipart/form-data',

                validateInitialCount: true,

                previewFileIcon: "<i class='glyphicon glyphicon-king'></i>",

                msgFilesTooMany: "选择上传的文件数量({n}) 超过允许的最大数值{m}！",
            });

            // 上传成功后处理方法
            fileInput.on("fileuploaded", function (event, data, previewId, index) {
                console.info(event)
                console.info(data)
                console.info(previewId)
                console.info(index)
                callBack && callBack(data.response);
            });
        },
        requestHtml: function (url, callbacke) {
            $.ajax({
                url: url,
                success: function (res) {
                    callbacke && callbacke(res);
                },
                error: function (jXHR, textStatus, errorThrown) {
                    JWB.http.errorHandle(jXHR, textStatus, errorThrown)
                }
            });
        },

        successHandle: function () {

        },
        errorHandle: function (jXHR, textStatus, errorThrown) {
            if (jXHR.status == 401) {
                alert("登录过期，跳转登录页面")
                // 提示登录过期
                location.href = base.basePath +"/login.jsp";
                // 跳转
            }
        }
    }
}


